package com.team5101.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.team5101.car.api.ICarService;
import com.team5101.entity.TUser;
import com.team5101.vo.ResultBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping("cart")
public class CartController {
    @Reference
    private ICarService cartService;

    @RequestMapping("add/{productId}/{count}")
    @ResponseBody
    public ResultBean addProduct(@CookieValue(name = "user_cart", required = false) String uuid,
                                 @PathVariable Long productId,
                                 @PathVariable int count,
                                 HttpServletResponse response,
                                 HttpServletRequest request){



        Object o = request.getAttribute("user");
        if(o!=null){
            //================已登录状态下的购物车======================= redis:    user:cart:userId
            TUser user = (TUser) o;
            Long userId = user.getId();
            return cartService.addProduct(userId.toString(),productId,count);

        }


        //==============未登录状态下的购物车=================
        //把该商品添加到购物车，这个购物车是在redis中。

        if(uuid==null||"".equals(uuid)){
            //uuid为空的话再生成一个uuid放到cookie里给客户端
            uuid = UUID.randomUUID().toString();
            //返回该uuid给cookie
            Cookie cookie = new Cookie("user_cart",uuid);
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        ResultBean resultBean = cartService.addProduct(uuid,productId,count);

        return resultBean;
    }


    @RequestMapping("show")
    @ResponseBody
    public ResultBean showCart(@CookieValue(name="user_cart",required = false)String uuid,HttpServletRequest request) {

        Object o = request.getAttribute("user");
        //============查看已登录状态下的购物车=============
        if (o != null) {
            TUser user = (TUser) o;
            Long userId = user.getId();
            return cartService.showCart(userId.toString());
        }
        //============查看未登录状态下的购物车=============
        return cartService.showCart(uuid);
    }

    @RequestMapping("clean")
    @ResponseBody
    public ResultBean cleanCart(@CookieValue(name="user_cart",required = false)String uuid,HttpServletResponse response,
                                HttpServletRequest request){
        Object o = request.getAttribute("user");
        if(o!=null){
            //===========已登录状态下的购物车============
            TUser user = (TUser) o;
            return cartService.clean(user.getId().toString());
        }
        //===========未登录状态下的购物车===========
        if(uuid!=null&&!"".equals(uuid)){
            //删除Cookie
            Cookie cookie = new Cookie("user_cart","");
            cookie.setMaxAge(0);
            cookie.setPath("/");//admin.qf.com  sso.qf.com  ****.qf.com
            // cookie.setDomain("qf.com");//如果我们使用域名来访问，那么cookie不会被携带，只有这边设置了这个一级域名，qf.com,那么在该域名下的所有二级cookie就都可以携带该cookie
            response.addCookie(cookie);

            //删除redis中的购物车
            return cartService.clean(uuid);
        }
        return ResultBean.error("当前用户没有购物车");

    }


}
